package br.com.viamais.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.viamais.infra.dao.BaseDAO;
import br.com.viamais.model.Profile;

/**
 * <B>Project: viamais-web</B><BR>
 * 
 * DAO that represents database operations of the Profile entity.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Oct 3, 2011
 * @version 1.0
 */
// TODO implement log.
public class ProfileDAO extends BaseDAO<Profile> {

	/**
	 * Serial Version ID.
	 */
	private static final long serialVersionUID = -1305765962572173369L;

	@Override
	protected Class<Profile> getClazz() {
		return Profile.class;
	}

	/**
	 * Get profile by name with anywhere search method.
	 * 
	 * @param String: name.
	 * 
	 * @return List<Profile>: profile list.
	 * 
	 * @throws Throwable: connections issues.
	 */
	@SuppressWarnings("unchecked")
	public List<Profile> getByName(String name) throws Throwable {
		List<Profile> profiles = null;
		if (name != null) {
			this.beginTransaction();
			Criteria criteria = this.getSession().createCriteria(getClazz());
			criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
			profiles = (List<Profile>) criteria.list();
			this.commit();
		}
		return profiles;
	}
	
	/**
	 * Get unique profile by name.
	 * 
	 * @param String: name.
	 * 
	 * @return Profile: profile.
	 * 
	 * @throws Throwable: connections issues.
	 */
	public Profile getUniqueByName(String name) throws Throwable {
		Profile profile = null;
		if (name != null) {
			this.beginTransaction();
			Criteria criteria = this.getSession().createCriteria(getClazz());
			criteria.add(Restrictions.ilike("name", name, MatchMode.EXACT));
			profile = (Profile) criteria.uniqueResult();
			this.commit();
		}
		return profile;
	}
}
